var http = require('http');
var cheerio = require('cheerio');
// var Promise = require('Promise'); // 低版本的nodejs可能没有这个模块
var Promise = require('bluebird');
var baseUrl = "http://www.imooc.com/learn/";
var videoIds = [197, 75, 259, 348, 637]


var fetchCourseArray = [];
videoIds.forEach(function (id) {
    fetchCourseArray.push(getPageAsync(baseUrl + id));
})

// 接收数组，一个一个Promise，
Promise
    .all(fetchCourseArray)
    .then(function (pages) {
        var coursesData = [];

        pages.forEach(function(html) {
            var courses = filterChapter(html);
            coursesData.push(courses);
        })
        coursesData.sort(function (a,b){
            return a.number < b.number; // 从大到小的排序
        })
        printCourseInfo(coursesData);
    })




    function getPageAsync(url) {
        return new Promise(function (resolve, reject) {
            console.log("正在爬取");

            http.get(url, function(res) {
                var html = '';

                res.on('data', function(data) {
                    html += data;
                })

                res.on('end', function() {
                    // console.log(html)
                    resolve(html);
                    // var courseData = filterChapter(html);
                    // printCourseInfo(courseData);
                })
            }).on('error', function() {
                reject(e);
                console.log('error!!!!');
            })
        })
    }












// 解析HTML
function filterChapter(html) {
    var $ = cheerio.load(html);
    var chapers = $('.chapter');
    var title = $('.course-infos .path span').text();
    console.log("title",title);

    // var number = parseInt($('.js-learn-num').text().trim(), 10)
    var number = parseInt($('.course-infos .statics .static-item:last-child').find('.meta-value strong').text().trim(),10)
    console.log("number",number);
// {
//     title: title,
//     number: number,
//     videos: [{
//         chapterTitle: '',
//         videos: [
//             title: '',
//             id: ''
//         ]
//     }]
// }
// https://github.com/Pearyman/moocDemos/blob/master/imooc/nodejs_base_02/1_3/promise_crawler.js


    var courseData = {
        title: title,
        number: number,
        videos: []
    };

    chapers.each(function(item) {
        var chapter = $(this);
        var chapterTitle = chapter.find('strong').text().replace(/\s+/g,' ');
        var videos = chapter.find('.video').children('li');

        var chaperData = {
            chapterTitle: chapterTitle,
            videos: []
        }

        videos.each(function(item) {
            var video = $(this).find('.J-media-item');

            var videoTitle = video.text().replace(/\s+/g,' ');
            var id = $(this).data("media-id");

            chaperData.videos.push({
                title: videoTitle,
                id: id
            })
        })
        // 在外侧push，否则有多条重复数据
        courseData.videos.push(chaperData);
    })

    return courseData;

}



function printCourseInfo(coursesData) {

    coursesData.forEach(function (courseData) {
        console.log("=========printInfo BEGIN=========");
        //  todo 需要根据当前页面爬取的内容进行打印
        console.log(courseData.number + '人');


        courseData.videos.forEach(function(item) {
            var chapterTitle = item.chapterTitle;
            console.log(chapterTitle + '\n');
            item.videos.forEach(function(video) {
                console.log('[' + video.id + ']' + video.title + '\n');
            })

        })
        console.log("=========printInfo END=========");
    })

}
